home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 26: Security / pc_actual_seguridad.iso / Encriptacion / XORcrypt 1.1 / XORcrypt.js < prev    next >
Encoding:
JavaScript  |  2001-10-30  |  7.3 KB  |  248 lines

  1. /*********************************************************************************
  2.  * Title:        XORcrypt
  3.  * Description:Simple script, allowing reliably encrypt/decrypt of any files with 
  4.  *                 the help of a key file and bitwise operation "exclusive OR" (XOR).
  5.  *                 It is impossible to do it more reliable and easier.
  6.  * Copyright:    Copyright (c) 2001
  7.  * @author        Alexandr Shpirkov 
  8.  * @version        1.3
  9.  ********************************************************************************/
  10.  
  11. var ForReading = 1, ForWriting = 2;
  12. // Please do not copy next line through clipboard. It can destroys the program.
  13. var ANSI0="ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ";
  14. var ANSI2="";
  15. var sName="",keyName="",oName="",tName,ask=true,another=false,onlyencrypt=false,crypt="encrypt",CRC=111,OFFSET=1111,testlength=30,prev="";
  16. var SS,KS,OS;
  17. var WshShell=WScript.CreateObject("WScript.Shell");
  18. var fso = WScript.CreateObject("Scripting.FileSystemObject");
  19. var MSCD = WScript.CreateObject("MSComDlg.CommonDialog");
  20.  
  21. testversion();
  22. for(i=0;i<128;i++) ANSI2+=ANSI0.charAt(i);
  23. getArgs();
  24. defineNames();
  25. testSource();
  26. if(ask==false || askSure()) work();
  27.  
  28. function XOR(SourceStream,KeyStream,OutStream) {
  29.     do {
  30.         try { 
  31. //------------------------------------------------------------------------------//
  32. //*** ATTENTION!!! The basic code  *********************************************//
  33. //    encrypted               = source               XOR     key                //
  34. // or source                  = encrypted            XOR     key                //
  35.         OutStream.Write(char(code(SourceStream.Read(1)) ^ code(KeyStream.Read(1))));
  36. //------------------------------------------------------------------------------//
  37.         } 
  38.         catch(e) {}
  39.     } while ( !SourceStream.AtEndOfStream )
  40. }
  41.  
  42. function work() {
  43.     tName=""+fso.GetParentFolderName(oName)+"\\"+fso.GetTempName();
  44.     OS=fso.OpenTextFile( tName, ForWriting, true);
  45.     KS=new KeyStream( keyName,OFFSET);
  46.     SS=fso.OpenTextFile( sName, ForReading, false);
  47.     prev="";
  48.     if(crypt=="encrypt") {
  49.         OS.Write("XR"+OFFSET);
  50.         var CRCS=new StrIStream(""+CRC);
  51.         XOR(CRCS,KS,OS);
  52.         XOR(SS,KS,OS);
  53.     }
  54.     else {
  55.         SS.Read(9); for(i=0;i<3;i++) KS.Read();
  56.         XOR(SS,KS,OS);
  57.     }
  58.     KS.Close();OS.Close();SS.Close();
  59.     fso.DeleteFile (oName);
  60.     fso.MoveFile ( tName, oName );
  61. }
  62.  
  63. function code(chr) {
  64. // JScript does not work with bytes. It is necessary to deceive.
  65.     var cnum=chr.charCodeAt(0);
  66.     if(cnum<256) return cnum;
  67.     return 128+ANSI2.indexOf(String.fromCharCode(cnum));
  68. }
  69.  
  70. function char(cnum) {
  71.     if(cnum>127 && cnum<256) return ANSI0.substr(cnum-128,1);
  72.     return String.fromCharCode(cnum);
  73. }
  74.  
  75. function p(s) {
  76.    WScript.echo(s);
  77. }
  78.  
  79. function FileGet(fName,flength) {
  80.     var ts = fso.GetFile( fName ).OpenAsTextStream(ForReading);
  81.     var ret="";
  82.     var count=0;
  83.     try { while(count++<flength) ret+=ts.Read(1); } catch(e) {}
  84.     ts.Close( );
  85.     return(ret);
  86. }
  87.  
  88. function getArgs() {
  89. var args=WScript.arguments;
  90. if(args.length==0) arglist();
  91. for (i=0;i<args.length;i++)  {
  92.     switch (args(i).substr(0,2).toUpperCase( )) {
  93.         case "/?" :
  94.             arglist();
  95.         case "/K" :
  96.           keyName=args(i).substr(3);break;
  97.         case "/O" :
  98.             another=true;oName=args(i).substr(3);break;
  99.         case "/E" :
  100.             onlyencrypt=true;    break;
  101.         case "/Y" :
  102.             ask=true;            break;
  103.         case "/N" :
  104.             ask=false;        break;
  105.         default :
  106.             if(args(i).substr(0,1)=="/") {
  107.               p("Unknown swith: "+args(i));
  108.                 WScript.Quit();
  109.             }
  110.             else if(sName.length==0) sName=args(i);
  111.     } 
  112. }
  113. }
  114.  
  115. function arglist() {
  116.       p("XORcrypt [source] [/K:keyfile] [/O[:outfile]] [/E] [/Y] [/N] [/?]"+
  117.       "\n\t source  - full path to source file. If it is omitted, it will be requested in dialogue."+
  118.       "\n\t keyfile - full path to key file. If it is omitted, it will be requested in dialogue."+
  119.       "\n\t outfile - full path to destination file. If it is omitted, it will be overwritten source file."+
  120.       "\n\t           If /O used without path, destination file will be requested in dialogue."+
  121.       "\n\t /E      - To encrypt file once more even if it is already encrypted."+
  122.       "\n\t           By default encrypted file will be decrypted."+
  123.      "\n\t /Y      - To carry out an encryption/decryption without confirmation."+
  124.       "\n\t /N      - The same with confirmation (default)."+
  125.       "\n\t /?      - To show options list.");
  126.     WScript.Quit();
  127. }
  128.  
  129. function defineNames() {
  130.     MSCD.Flags = 4;
  131.     MSCD.MaxFileSize =200;
  132.     MSCD.CancelError =true;
  133.     if(sName.length==0)            sName=getname("source");
  134.     if(keyName.length==0)        keyName=getname("key");
  135.     if(another ) {    if(oName.length==0)    oName=getname("destination"); }
  136.     else oName=sName;
  137. }
  138.  
  139. function getname(kid) {
  140.     MSCD.DialogTitle ="Select "+kid+" file";
  141.     MSCD.FileName="";
  142. // Cancel (Esc) will interrupt the program
  143.     try { MSCD.ShowOpen(); } catch(e) { nocrypt();}
  144.     return MSCD.FileName;
  145. }
  146.  
  147. function askSure() {
  148.     if(WshShell.Popup("You really want to "+crypt+" \n"+sName+"\n with key file \n"+keyName,1000,"",36)==6)    return true;
  149.     nocrypt();
  150.     return false;
  151. }
  152.  
  153. function testSource() {
  154.     OFFSET=parseInt(Math.random( )*8999)+1000;
  155.     var test=FileGet(sName,testlength+9);
  156.     if(onlyencrypt) { CRC=defCRC(test); return;}
  157.     if(test.substr(0,2)=="XR") {
  158.         OFFSET=0+test.substr(2,4);
  159.         SS= new StrIStream(test.substr(6,testlength+3));
  160.          OS= new StrOStream();
  161.         KS= new KeyStream(keyName,OFFSET);
  162.         XOR(SS,KS,OS);
  163.         var dectypted=OS.value;
  164.         KS.Close();
  165.         crypt="decrypt";
  166.         if (0+dectypted.substr(0,3)!=defCRC(dectypted.substr(3))) {
  167.             var WshShell=WScript.CreateObject("WScript.Shell");
  168.             WshShell.Popup("Sorry. Decryption is impossible.\n May be you encrypt \n"+sName+"\n with another key file than \n"+keyName,1000,"",16);
  169.             nocrypt();
  170.         }
  171.     }
  172.     else { CRC=defCRC(test.substr(0,testlength)); return;}
  173. }
  174.  
  175. function defCRC(csstr) {
  176.     var mCRC=0;
  177.     for(i=0;i<csstr.length;i++) mCRC+=csstr.charCodeAt(i);
  178.     return (mCRC % 899)+100;
  179. }
  180.  
  181. function nocrypt() {
  182.     p("File "+sName+" is not "+crypt+"ed.");
  183.     WScript.Quit();
  184. }
  185.  
  186. function KeyStream(fName,offset) {
  187.     this.file = fso.GetFile( fName );
  188.     this.ts = this.file.OpenAsTextStream(ForReading);
  189.     this.Read =KSRead;
  190.     this.Close=KSClose;
  191.     var toffset=offset % this.file.Size;
  192.     if( toffset >0 ) this.ts.Read(toffset);
  193.     return this;
  194. }
  195.  
  196. function StrOStream() {
  197.     this.value  = new String("");
  198.     this.Write=SWrite;
  199.     this.Close=Empty;
  200. }
  201.  
  202. function StrIStream(istr) {
  203.     this.value = new String(istr);
  204.     this.index=0;
  205.     this.AtEndOfStream=false;
  206.     this.Read=SRead;
  207.     this.Close=Empty;
  208.     return this;
  209. }
  210.  
  211. function Empty() {}
  212.  
  213. function KSRead() {
  214.     do {
  215.         try { ret = this.ts.Read(1); } 
  216.         catch(e) {
  217.             this.ts.Close();
  218.             this.ts = this.file.OpenAsTextStream(ForReading);
  219.             ret = prev;
  220.         }
  221.     } while ( ret==prev || ret.charCodeAt(0)==0 )
  222.     prev=ret;
  223.     return ret;
  224. }
  225.  
  226. function KSClose() {
  227.     this.ts.Close();
  228. }
  229.  
  230. function SWrite(istr) {
  231.  this.value= this.value.concat(istr);
  232. }
  233.  
  234. function SRead() {
  235.     this.index++;
  236.     if(this.index>this.value.length) { this.AtEndOfStream=true; throw "end of line";   ;}
  237.     return this.value.charAt(this.index-1);
  238. }
  239.  
  240. function testversion() {
  241.     if(ScriptEngineMajorVersion()<5) {
  242.         var URL="http://www.microsoft.com/msdownload/vbscript/scripting.asp";
  243.         if(WshShell.Popup("Excuse, but you yours Script Engine has too old Version."+
  244.             "\nPlease update on "+URL,1000,"",17)==1) WshShell.Run(URL);
  245.         WScript.Quit();
  246.     }
  247. }
  248.